<template>
  <div class="app-container">
    <h4 class="form-header h4">基本信息</h4>
    <t-form :data="form" label-width="80px">
      <t-row>
        <t-col :span="4" :offset="2">
          <t-form-item label="用户昵称" name="nickName">
            <t-input v-model="form.nickName" disabled />
          </t-form-item>
        </t-col>
        <t-col :span="4" :offset="2">
          <t-form-item label="登录账号" name="userName">
            <t-input v-model="form.userName" disabled />
          </t-form-item>
        </t-col>
      </t-row>
    </t-form>

    <h4 class="form-header h4">角色信息</h4>
    <t-table
      v-loading="loading"
      row-key="roleId"
      ref="roleRef"
      :columns="columns"
      selectOnRowClick
      @select-change="handleSelectionChange"
      :data="roles.slice((pageNum - 1) * pageSize, pageNum * pageSize)"
    >
      <template #serial="scope">
        <span>{{ (pageNum - 1) * pageSize + scope.rowIndex + 1 }}</span>
      </template>
      <template #createTime="scope">
        <span>{{ parseTime(scope.row.createTime) }}</span>
      </template>
    </t-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="pageNum"
      v-model:limit="pageSize"
    />

    <t-form label-width="100px">
      <div style="text-align: center; margin-left: -120px; margin-top: 30px">
        <t-space>
          <t-button theme="primary" @click="submitForm()">提交</t-button>
          <t-button theme="default" @click="close()">返回</t-button>
        </t-space>
      </div>
    </t-form>
  </div>
</template>

<script setup name="AuthRole">
import { getAuthRole, updateAuthRole } from "@/api/system/user";

const route = useRoute();
const { proxy } = getCurrentInstance();

const loading = ref(true);
const total = ref(0);
const pageNum = ref(1);
const pageSize = ref(10);
const roleIds = ref([]);
const roles = ref([]);
const form = ref({
  nickName: undefined,
  userName: undefined,
  userId: undefined,
});

const columns = ref([
  { colKey: "serial", title: "序号", align: "center", width: 65 },
  {
    colKey: "row-select",
    title: "选择",
    type: "multiple",
    align: "center",
    width: 65,
  },
  {
    colKey: "roleId",
    title: "角色编号",
    align: "center",
  },
  {
    colKey: "roleName",
    title: "角色名称",
    align: "center",
  },
  {
    colKey: "roleKey",
    title: "权限字符",
    align: "center",
  },
  {
    colKey: "createTime",
    title: "创建时间",
    align: "center",
    width: 180,
  },
]);

/** 多选框选中数据 */
function handleSelectionChange(selectedRowKeys) {
  roleIds.value = selectedRowKeys;
}
/** 关闭按钮 */
function close() {
  const obj = { path: "/system/user" };
  proxy.$tab.closeOpenPage(obj);
}
/** 提交按钮 */
function submitForm() {
  const userId = form.value.userId;
  const rIds = roleIds.value.join(",");
  updateAuthRole({ userId: userId, roleIds: rIds }).then((response) => {
    proxy.$modal.msgSuccess("授权成功");
    close();
  });
}

(() => {
  const userId = route.params && route.params.userId;
  if (userId) {
    loading.value = true;
    getAuthRole(userId).then((response) => {
      form.value = response.user;
      roles.value = response.roles;
      total.value = roles.value.length;
      nextTick(() => {
        roles.value.forEach((row) => {
          if (row.flag) {
            roleIds.value.push(row.roleId);
          }
        });
      });
      loading.value = false;
    });
  }
})();
</script>
